<template>
  <a-spin tip="导入中..." :spinning="importing">
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目类型">
              <j-dict-select-tag v-model="queryParam.xmlxid" placeholder="请选择项目类型" dictCode="hydm"></j-dict-select-tag>
             <!-- <a-select style="width: 100%"  v-model="queryParam.xmlxid">
                <a-select-option v-for="d in xmlxDatas" :key="d.value" :value="d.value">{{d.text}}</a-select-option>
              </a-select>-->
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目状态">
              <j-dict-select-tag v-model="queryParam.status" placeholder="请选择项目状态" dictCode="project_status_pt"/>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目编号">
              <j-input placeholder="请输入项目编号" v-model="queryParam.xmbh"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="企业名称">
              <j-input placeholder="请输入企业名称" v-model="queryParam.gsmc"></j-input>
            </a-form-item>
          </a-col>
          <!--<a-col :xl="5" :lg="7" :md="8" :sm="24">-->
          <!--  <a-form-item label="所属驿站">-->
          <!--    <j-dict-select-tag v-model="queryParam.yzmc" placeholder="请选择所属驿站" dictCode="lhyg_corpinfo,corpname,id,qylx='1' order by corpname"/>-->
          <!--  </a-form-item>-->
          <!--</a-col>-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="所属驿站">
              <j-search-select-tag placeholder="请输入驿站名称在选择" v-model="queryParam.yzmc" dict="lhyg_corpinfo_yz,corpname,id" :async="true">
              </j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!--<div class="table-operator" >-->
    <!--      <a-upload v-show="true" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
    <!--                @change="handleImportExcel">-->
    <!--        <a-button type="primary" icon="import">导入企业项目</a-button>-->
    <!--      </a-upload>-->
    <!--      <a-button type="primary" icon="download"><a href="http://192.168.0.195:9005/mb/企业项目信息列表.xls" download="企业项目信息列表.xls" style="color: white">下载模板</a></a-button>-->
    <!--      <a-upload v-show="true" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
    <!--                @change="handleImportExcel">-->
    <!--        <a-button type="primary" icon="import">导入企业项目</a-button>-->
    <!--      </a-upload>-->
    <!--      <a-button type="primary" icon="download"><a href="http://192.168.0.195:9005/mb/企业项目信息列表.xls" download="企业项目信息列表.xls" style="color: white">下载模板</a></a-button>-->
    <!--      <a-upload v-show="true" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
    <!--                @change="handleImportExcel">-->
    <!--        <a-button type="primary" icon="import">导入企业项目</a-button>-->
    <!--      </a-upload>-->
    <!--      <a-upload v-show="true" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importDjsExcelUrl"-->
    <!--                @change="handleImportExcel">-->
    <!--        <a-button type="primary" icon="import">导入待结算企业项目</a-button>-->
    <!--      </a-upload>-->
    <!--      <a-button type="primary" icon="download"><a href="http://192.168.0.195:9005/mb/企业项目信息列表.xls" download="企业项目信息列表.xls" style="color: white">下载模板</a></a-button>-->
    <!--    </div>-->

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
      <!--</div>-->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.status >= 2" @click="handleDetail(record)">详情</a>
<!--          <a v-if="record.status == 2" @click="handleEdit(record)">编辑</a>-->
          <a-divider type="vertical" v-if="record.status == 2" />
          <a-popconfirm title="确定发布吗?" @confirm="() => handleChange(record.id,4)" v-if="record.status == 2">
            <a>发布</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.status == 2" />
          <a-popconfirm title="确定驳回吗?" @confirm="() => refuseReason(record)" v-if="record.status == 2">
            <a>驳回</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.isSign == 1" />
          <a v-if="record.isSign == 1" @click="downloadFile(record.htpdf)">合同</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleChange(record.id,-1)">
            <a>删除</a>
          </a-popconfirm>
          <!--<a-divider type="vertical" v-if="record.status > 2" />-->
          <!--<a v-if="record.status > 2" @click="viewXmxy(record)">项目协议</a>-->
        </span>
      </a-table>

      <a-modal
        title="提示"
        :visible="visibleReason"
        :confirm-loading="false"
        @ok="handleOkReason"
        @cancel="handleCancelReason"
      >
        <a-form-item label="拒绝理由" >
          <a-textarea placeholder="请输入50字以内..." :rows="4" v-model="reason" maxLength="50" />
        </a-form-item>

      </a-modal>

      <a-modal
        title="合同"
        :visible="visibleHt"
        width="1000px"
        okText="确认签署"
        cancelText="关闭"
        @ok="ptSign"
        @cancel="htClose"
        :confirm-loading="spinning"
      >
        <a-spin tip="签署中，请稍等..." :spinning="spinning">
          <div id="ht" style="width:960px;height:600px;">
            <iframe style="width: 100%;height: 100%;" :src="htImgUrl"></iframe>
          </div>
        </a-spin>
      </a-modal>
    </div>

    <a-modal
      title="查看项目协议"
      :visible="visibleHz"
      width="1000px"
      cancelText="关闭"
      @cancel="HzClose"
      :okButtonProps="{ class:{'jee-hidden': true} }"
    >
      <div  id="hz" style="width:960px;height:600px;">
        <iframe style="width: 100%;height: 100%;" :src="hzImgUrl"></iframe>
      </div>
    </a-modal>

    <lhyg-project-modal ref="modalForm" @ok="modalFormOk"></lhyg-project-modal>
  </a-card>
  </a-spin>
</template>

<script>

  import { httpAction, getAction, postAction } from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import LhygProjectModal from './modules/LhygProjectModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JInput from '@/components/jeecg/JInput'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  let recordBean;
  export default {
    name: 'LhygProjectList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JDictSelectTag,
      LhygProjectModal,
      JInput,
      JSearchSelectTag
    },
    data () {
      return {
        description: '企业项目管理页面',
        reason:'', // 拒绝理由
        records:[], // 通过和拒绝的数据
        visibleReason:false, // 拒绝理由控制隐藏显示
        queryParam: {
            xmlx: '',
            status: '',
            xmbh: '',
        },
        // 表头
        columns: [
          {
            title:'企业名称',
            align:"center",
            dataIndex: 'gsmc'
          },
          {
            title:'项目编号',
            align:"center",
            dataIndex: 'xmbh',
            customRender:function (text,record, index) {
                if(record.status < 1) {
                    return ""
                } else {
                    return text
                }
            }
          },
          {
            title:'项目协议编号',
            align:"center",
            dataIndex: 'xmxybh',
            customRender:function (text,record, index) {
                if(record.status < 1) {
                    return ""
                } else {
                    return text
                }
            }
          },
          {
            title:'项目名称',
            align:"center",
            dataIndex: 'xmmc'
          },
          {
            title:'项目类型',
            align:"center",
            dataIndex: 'xmlxid_dictText'
          },
          // {
          //     title:'任务总金额',
          //     align:"center",
          //     dataIndex: 'rwzje'
          // },
          {
            title:'单人结算金额',
            align:"center",
            dataIndex: 'drjsje'
          },
          {
            title:'需要人数',
            align:"center",
            dataIndex: 'xyrs'
          },
          {
            title:'已报名人数',
            align:"center",
            dataIndex: 'ybmrs'
          },
          {
            title:'已录用人数',
            align:"center",
            dataIndex: 'ylyrs'
          },
          {
            title:'项目起止日期',
            align:"center",
            dataIndex: 'xmqrq',
            customRender:function (text,record, index) {
                return record.xmqrq + "~" + record.xmzrq;
            }
          },
          {
            title:'工作时间',
            align:"center",
            dataIndex: 'gzsj',
            customRender:function (text,record, index) {
                let t = "";
                if(record.gzkssj != undefined){
                    t = t + record.gzkssj;
                }
                if (record.gzkssj != undefined && record.gzjssj != undefined){
                    t = t + "~";
                }
                if (record.gzjssj != undefined){
                    t = t + record.gzjssj;
                }
                return t;
            }
          },
          {
            title:'工作城市',
            align:"center",
            dataIndex: 'gzcsmc',
          },
          {
            title:'项目状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'发起时间',
            align:"center",
            dataIndex: 'fbsj',
            customRender:function (text) {
                return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ptproject/lhygProject/list",
          updateStatus: "/ptproject/lhygProject/updateStatus",
          // getXmlxDatas: "project/lhygProject/getXmlxDatas",
          ptSign: "/ptproject/lhygProject/ptSign",
          getErrSkr: "/project/lhygProject/getErrSkr",
          importExcelUrl:"ptproject/lhygProject/importSkrExcel",
          importDjsExcelUrl:"ptproject/lhygProject/importProjectExcel",
        },
        dictOptions:{},
        visibleHt: false,
        spinning: false,
        importing: false,
        pageNum: 1,
        pageTotalNum: 1,
        loadedRatio: 0,
        htImgUrl:'',
        smscode: '',
        recordId: '',
        // xmlxDatas:[], //接收项目类型数据的对象
        visibleHz: false,
        hzImgUrl: '',
      }
    },
    created() {
      // this.fetchXmlxData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      importDjsExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importDjsExcelUrl}`;
      },
    },
    methods: {
      handleOkReason(){
        let that = this;
        let formData=new FormData();
        formData.append('id',that.records.id);
        formData.append('status','1');
        formData.append('reason',that.reason);
        postAction("/ptproject/lhygProject/updateStatus",formData).then(res => {
          if(res.code==200){
            that.$message.success(res.message);
            that.searchQuery();
            that.visibleReason = false
          }else{
            that.$message.error(res.message)
          }
        }).catch(err => {
          that.$message.success("审核失败")
        });
      },
      handleCancelReason(){
        this.visibleReason = false
      },
      refuseReason(record){
        let that = this;
        that.records = record;
        that.reason = '';
        that.visibleReason = true
      },
      initDictConfig(){
      },
      // fetchXmlxData(){
      //     getAction(this.url.getXmlxDatas, null).then((res)=>{
      //         if(res.success){
      //             const result = res.result
      //             result.forEach((r)=>{
      //                 this.xmlxDatas.push({
      //                     value:r.value,
      //                     text:r.text
      //                 })
      //             })
      //         }
      //     });
      // },
      handleDetail(record){
          this.$refs.modalForm.detail(record);
          this.$refs.modalForm.title="详情";
          this.$refs.modalForm.disableSubmit = true;
      },
      // 上一页
      prePage() {
          let page = this.pageNum
          page = page > 1 ? page - 1 : this.pageTotalNum
          this.pageNum = page
          document.getElementById('ht').scrollTop = 0;
      },

      // 下一页
      nextPage() {
          let page = this.pageNum
          page = page < this.pageTotalNum ? page + 1 : 1
          this.pageNum = page
          document.getElementById('ht').scrollTop = 0;
      },
      htShow(record){
          recordBean = record;
          const that = this;
          let formData=new FormData();
          formData.append('id',record.id);
          formData.append('xmlxid',record.xmlxid);
          formData.append('type','2');
          postAction(that.url.getErrSkr, formData).then(res => {
              if (res.success) {
                  if(res.result == '1'){
                      that.spinning = false
                      this.$message.warning("未指派任何任务接收者，无法发布");
                  } else if(res.result == '3'){
                      that.spinning = false
                      this.$message.warning("任务接收者列表中存在异常用户（税务已注销），无法发布");
                  } else if(res.result == '5'){
                      that.spinning = false
                      this.$message.warning("任务接收者列表中存在异常用户（黑名单），无法发布");
                  }  else if(res.result == '4'){
                      that.spinning = false
                      this.$message.warning("任务接收者列表中，存在重复用户，无法发布");
                  } else {
                      that.htImgUrl = record.htpdf;
                      this.visibleHt = true;
                      this.pageNum = 1
                  }
              }
          }).catch((e) => {
              this.$message.error('校验接收者列表时异常');
          });
      },
      htClose(){
          this.spinning = false;
          this.visibleHt = false
      },
      ptSign(){
        const that = this;
        that.spinning = true;
        postAction(that.url.ptSign, recordBean).then((res) => {
          if (res.success){
            that.handleChange(recordBean.id,4);
            that.$message.success("发布成功");
            setTimeout(function () {
              that.visibleHt = false;
              that.spinning = false;
              that.loadData()
            }, 1400)
          }else{
            that.spinning = false;
            that.$message.error("发布失败")
          }
        });
      },
      viewXmxy(record){
          this.hzImgUrl = record.htpdf;
          this.visibleHz = true;
      },
      HzClose(){
          this.visibleHz = false;
      },
      handleImportExcel(info){
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        this.importing = true;
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            this.importing = false;
            // this.$message.success(`${info.file.name} 文件上传成功`);
            if (info.file.response.code === 201) {
              let { message, result: { msg, fileUrl, fileName } } = info.file.response
              let href = window._CONFIG['domianURL'] + fileUrl
              this.$warning({
                title: message,
                content: (
                    <div>
                      <span>{msg}</span><br/>
                      <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
                    </div>
                )
              })
            } else {
              this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            }
            this.loadData()
          } else {
            this.importing = false;
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.importing = false;
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
